﻿using System.Collections.Generic;
using Assets.Scripts.Net;
using Assets.Scripts.Net.Data;
using Assets.Scripts.Net.Data.Model;
using Assets.Scripts.UI.Buttons;
using Assets.Scripts.UI.Item;
using UnityEngine;
using Button = UnityEngine.UI.Button;

namespace Assets.Scripts.UI.Panel
{
    /// <summary>
    /// 查询分数的类型
    /// </summary>
    public enum CheckType
    {
        Null=-1,
        HomeWork=0,//作业
        Mock=1,//模拟考试
        Exam=2,//考试
    }

    /// <summary>
    /// 查询分数的Panel
    /// </summary>
    public class CheckScorePanel:UIBase<CheckScorePanel>
    {
        public Transform Content_Course;//课程的容器
        public Transform Content_Item;//试卷 作业 容器
        public Button ExitButton;//退出按钮
        public Button MockButton;//模拟考
        public Button ExamButton;//考试
        public Button HomeWorkButton;//家庭作业 
        public Button SettingButton;

        /// <summary>
        /// 当前点击的课程Item
        /// </summary>  
        public static CourseItem NowClickCourseItem { get; set; }

        /// <summary>
        /// 当前点击的标签按钮
        /// </summary>
        private Button nowClickButton { get; set; }

        /// <summary>
        /// 查询类型
        /// </summary>
        public CheckType CheckType { get; set; }

        protected override UIType UIType
        {
            get { return UIType.Panel; }
        }

        public static CheckScorePanel _Instance;

        protected override void InitOnDestroy()
        {
            _Instance = null;
        }

        protected override void InitOnAwake()   
        {   
            _Instance = this;
            AutoFindComponent(transform,true);
            ExitButton.onClick.AddListener(Exit);
            MockButton.onClick.AddListener(()=>OnButtonClick(CheckType.Mock,MockButton));
            ExamButton.onClick.AddListener(()=>OnButtonClick(CheckType.Exam,ExamButton));
            HomeWorkButton.onClick.AddListener(()=>OnButtonClick(CheckType.HomeWork,HomeWorkButton));
            SettingButton.onClick.AddListener(OnSettingButtonClick);
            ExamButton.onClick.Invoke();
        }

        private void OnSettingButtonClick()
        {
            CreatePanel(PanelType.SettingPanel);
        }

        /// <summary>
        /// 点击事件
        /// </summary>
        private void OnButtonClick(CheckType type,Button button)
        {         
            if(nowClickButton==button)return;
            if (nowClickButton != null)
            {
                nowClickButton.GetComponent<ButtonSprite>().SetNormalSprite();
            }
            nowClickButton = button;
            button.GetComponent<ButtonSprite>().SetPressedSprite();
            button.transform.SetAsLastSibling();
            Dictionary<string, object> dict = NetManager.Instance.GetDict();
            CheckType = type;
            dict.Add("type",(int)type);
            CleanTransformChildren(Content_Course);
            CleanTransformChildren(Content_Item);
            NetManager.Instance.SendRequest(RequestType.GetScore, dict,() =>
            {
                CleanTransformChildren(Content_Course);
                CleanTransformChildren(Content_Item);
                List<CourseInfo> courses =
                    NetReaderFactory.Instance()
                        .GetInfoByType<List<CourseInfo>>() as List<CourseInfo>;
                NetReaderFactory.Instance().GetInfoByType<List<PaperInfo>>();
                Init(courses,type);
            });
        }        
        private void Init(List<CourseInfo> courseInfos,CheckType type)
        {
            for (int i = 0; i < courseInfos.Count; i++)
            {
                Transform tsf = CreateItem(ItemType.CourseItem);
                tsf.SetParent(Content_Course);
                tsf.localScale=Vector3.one;
                tsf.localPosition=Vector3.zero;
                CourseItem item = tsf.GetComponent<CourseItem>();
                item.Init(courseInfos[i], NetConstValue.CS_CourseInfo, new[] {Content_Item}, type);
                if(i==0)item.CourseButton.onClick.Invoke();
            }
        }
    }
}